package com.promis.okhttp;

import com.promis.util.CollectionUtils;
import com.promis.util.DateUtils;
import com.promis.util.StringUtils;

import java.nio.charset.Charset;
import java.util.Date;

import okhttp3.FormBody;

/**
 * Created by jidanbo on 2018/1/24.
 */

public class HttpStringParams {

    private FormBody.Builder builder;

    public HttpStringParams() {
        builder = new FormBody.Builder(Charset.forName("utf-8"));
    }

    public void add(String key, String value) {
        if (!StringUtils.isNullOrEmpty(value)) {
            builder.add(key, value);
        }
    }

    public void add(String key, String[] values) {
        if (!CollectionUtils.isNullOrEmpty(values)) {
            for (String value : values) {
                if (!StringUtils.isNullOrEmpty(value)) {
                    builder.add(key, value);
                }
            }
        }
    }

    public void add(String key, Number value) {
        if (value != null) {
            builder.add(key, value.toString());
        }
    }

    public void add(String key, Number[] values) {
        if (!CollectionUtils.isNullOrEmpty(values)) {
            for (Number value : values) {
                if (value != null) {
                    builder.add(key, value.toString());
                }
            }
        }
    }

    public void add(String key, Boolean value) {
        if (value != null) {
            builder.add(key, value.toString());
        }
    }

    public void add(String key, Boolean[] values) {
        if (!CollectionUtils.isNullOrEmpty(values)) {
            for (Boolean value : values) {
                if (value != null) {
                    builder.add(key, value.toString());
                }
            }
        }
    }

    public void add(String key, Date value) {
        if (value != null) {
            builder.add(key, DateUtils.format(value));
        }
    }

    public FormBody.Builder getBuilder() {
        return builder;
    }
}
